package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.object.pojo.AddCountDeposit;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AddCountDepositService;
import com.tbit.uqbike.service.business.OperaPswService;
import com.tbit.uqbike.webmanager.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chenmengran
 * @Description: 押金设置
 * @date 2019\12\27 002710:41
 */
@Validated
@RestController
@RequestMapping(value = "/deposit", produces = BaseController.MEDIA_JSON_UTF8)
public class AddCountDepositController extends BaseController {

    @Autowired
    private AddCountDepositService addCountDepositService;
    @Autowired
    private OperaPswService operaPswService;

    @RequestMapping("/setFee")
    @OperateLog(module = ModuleEnum.DEPOSIT, methods = MethodEnum.DEPOSIT_ADD)
    public JsonResult setFee(AddCountDeposit addCountDeposit, String operaPsw, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (!loginInfo.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(addCountDeposit.getAccountId(), "ver.accountId.notNull");
        } else {
            addCountDeposit.setAccountId(loginInfo.id);
        }

        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginInfo, OperaPswEnum.OPERA_PSW_SYSTEM_CONFIG.getModule(), operaPsw);

        addCountDepositService.add(loginInfo, addCountDeposit);

        return JsonResult.ok();
    }

    @RequestMapping("/getFee")
    public JsonResult getFee(Integer accountId, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (!loginInfo.type.equals(UserConstant.USER_AGENT)) {
            Assert.notNull(accountId, "ver.accountId.notNull");
        } else {
            accountId = loginInfo.id;
        }
        AddCountDeposit addCountDeposit = addCountDepositService.getByAccountId(loginInfo, accountId);

        return JsonResult.ok(addCountDeposit);
    }
}
